package com.ymdx.action;

import com.opensymphony.xwork2.ModelDriven;
import com.ymdx.entity.Course;
import com.ymdx.service.CourseService;
import com.ymdx.util.Page;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 课程
 * Created by yangliwei on 2017/8/22.
 */
public class CourseAction extends GenericActionSupport implements ModelDriven<Course>{
    private Course course;

    @Resource
    private CourseService courseService;

    private Page<Course> page;
    private Integer pageSize = 9;
    private Integer pageNo = 1;

    /**
     * 获取课程 列表
     * @return
     */
    @Action(value = "/course/list", results = {@Result(type = JSON, name = SUCCESS, params = {
            "root", "responseTool",
            "includeProperties", "status,code,message,data\\[\\d+\\]\\.id,data\\[\\d+\\]\\.name"
    })})
    public String list() {
        Course param = new Course();
        List<Course> courses = courseService.getByExample(param);
        responseTool.setStatus(1);
        responseTool.setData(courses);
        return SUCCESS;
    }

    /**
     * 保存课程
     * @return
     */
    @Action(value = "/admin/course/save", results = {@Result(type = JSON, name = SUCCESS, params = {
            "root", "responseTool",
            "includeProperties", "status,code,message,data\\.id"
    })})
    public  String save(){
        if(course.getName() == null){
            responseTool.setStatus(0);
            responseTool.setCode("parameter_empty");
            return SUCCESS;
        }
        course.setCreateTime(new Date());
        courseService.save(course);
        responseTool.setStatus(1);
        responseTool.setData(course);
        return SUCCESS;
    }

    @Action(value = "/admin/course/delete", results = {@Result(type = JSON, name = SUCCESS, params = {
            "root", "responseTool",
            "includeProperties", "status,code,message,data\\.id"
    })})
    public String delete(){
        Long id = course.getId();
        if(id == null){
            responseTool.setStatus(0);
            responseTool.setCode("parameter_empty");
            return SUCCESS;
        }
        Course oldCourse = courseService.getById(Course.class,id);
        if(oldCourse == null){
            responseTool.setStatus(0);
            responseTool.setCode("course_not_exist");
            return SUCCESS;
        }
        courseService.delete(oldCourse);
        return SUCCESS;
    }




    @Override
    public Course getModel() {
        if(course==null){
            course = new Course();
        }
        return course;
    }

    public Page<Course> getPage() {
        return page;
    }

    public void setPage(Page<Course> page) {
        this.page = page;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }
}
